﻿using System;
using System.Collections.Generic;

namespace _09_Composite
{
    /// <summary>
    /// 组合模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            AbstractFile all = new Folder("all");

            AbstractFile txt = new File("a.txt");
            AbstractFile exe = new File("qq.exe");
            all.Add(txt);
            all.Add(exe);
            all.PrintName();
            Console.ReadKey();
        }
    }
    abstract class AbstractFile
    {
        protected string filename;
        public AbstractFile(string name)
        {
            filename = name;
        }
        public abstract void Add(AbstractFile af);
        public abstract void Remove(AbstractFile af);
        public abstract void PrintName();

        public void Operation()
        {

        }

    }
    class File : AbstractFile
    {
        public File(string name):base(name)
        {

        }
        public override void Add(AbstractFile af)
        {
            Console.WriteLine("Add a file");
        }
        public override void Remove(AbstractFile af)
        {
            Console.WriteLine("Remove a file");
        }
        public override void PrintName()
        {
            Console.WriteLine("Print a file name:{0}",filename);
        }
    }
    class Folder : AbstractFile
    {
        List<AbstractFile> afs = new List<AbstractFile>();
        public Folder(string name):base(name)
        {

        }
        public override void Add(AbstractFile af)
        {
            afs.Add(af);
        }
        public override void Remove(AbstractFile af)
        {
            afs.Remove(af);
        }
        public override void PrintName()
        {
            Console.WriteLine(filename);
            foreach (AbstractFile af in afs)
            {
                af.PrintName();
            }
        }
    }
}
